package com.zlll.winner.service.media;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.media.MediaVideoList;
import com.zlll.winner.business.model.sysUser.SysUser;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.enums.bbs.CommentEnum;
import com.zlll.winner.mapper.media.MediaVideoListMapper;
import com.zlll.winner.business.service.media.IMediaVideoListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电台节目列表 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-10-17
 */
@Service
public class MediaVideoListServiceImpl extends ServiceImpl<MediaVideoListMapper, MediaVideoList> implements IMediaVideoListService {

    @Autowired
    private MediaVideoListMapper mediaVideoListMapper;

    /**
     * 获取视频分页列表数据
     * @param params
     * @return
     */
    @Override
    public IPage<Map<String, Object>> fetchVideoListPage(JSONObject params) {
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        String showTopic = params.getString("showTopic");
        Long menuId = params.getLong("menuId");
        Integer status = params.getInteger("status");
        Long createTime = params.getLong("createTime");
        IPage<Map<String, Object>> page = new Page<>(pageNo, pageSize);
        List<Map<String, Object>> list = mediaVideoListMapper.fetchVideoListPage(showTopic, menuId, createTime, status, page);
        page.setRecords(list);
        return page;
    }

    /**
     * 新增视频
     * @param mediaVideoList
     * @param user
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public boolean saveVideo(MediaVideoList mediaVideoList, SysUser user) {
        mediaVideoList.setCreateTime(System.currentTimeMillis());
        mediaVideoList.setIsDel(CommonEnum.YesOrNo.NO.val());
        mediaVideoList.setCreateUser(user.getName());
        mediaVideoList.setStatus(CommentEnum.StatusEnum.PUT_AWAY.val());
        Integer result = mediaVideoListMapper.insert(mediaVideoList);
        //新增记录为零
        if (result == 0) {
            return true;
        }
        return false;
    }

    /**
     * 编辑视频
     * @param mediaVideoList
     * @param user
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void editVideo(MediaVideoList mediaVideoList, SysUser user) {
        mediaVideoList.setUpdateUser(user.getName());
        mediaVideoList.setUpdateTime(System.currentTimeMillis());
        mediaVideoListMapper.updateById(mediaVideoList);
    }

    /**
     * 删除视频
     * @param id
     * @param name
     * @return
     */
    @Override
    public int deleteVideoById(Long id, String name) {
        MediaVideoList mediaVideoList = new MediaVideoList();
        mediaVideoList.setId(id);
        mediaVideoList.setUpdateUser(name);
        mediaVideoList.setUpdateTime(System.currentTimeMillis());
        mediaVideoList.setIsDel(CommonEnum.DelEnum.DEL.val());
        return mediaVideoListMapper.updateById(mediaVideoList);
    }

    /**
     * 下架功能
     * @param id
     * @param name
     * @return
     */
    @Override
    public int updateVideoStatusById(Long id, String name) {
        MediaVideoList mediaVideoList = new MediaVideoList();
        mediaVideoList.setId(id);
        mediaVideoList.setUpdateUser(name);
        mediaVideoList.setUpdateTime(System.currentTimeMillis());
        mediaVideoList.setStatus(CommentEnum.StatusEnum.PUT_BACK.val());
        return mediaVideoListMapper.updateById(mediaVideoList);
    }

    /**
     * 查询最新的一个视频
     * @return
     */
    @Override
    public MediaVideoList fetchVideoLast() {
        MediaVideoList mediaVideoList = null;
        QueryWrapper<MediaVideoList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", CommonEnum.YesOrNo.NO.val());
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("create_time");
        List<MediaVideoList> list = mediaVideoListMapper.selectList(queryWrapper);
        if(list!=null && list.size()>0){
            mediaVideoList = list.get(0);
        }
        return mediaVideoList;

    }
}
